'use strict';
// 验证登录
module.exports = (options, app) => {
  return async function checkLoginMiddleware(ctx, next) {
    const active_url = ctx.request.path;
    const active_res = active_url.split('/');
    const module_name = active_res[1];
    let login_id = 0;
    switch (module_name) {
      case 'admin':
        login_id = ctx.session.admin_id;
        break;
      case 'member':
        login_id = ctx.session.member_id;
        break;
      default:break;
    }
    let flag = !!login_id;
    if (!flag) {
      const allow_active_list = options.allow_active[module_name];
      for (let allow_active of allow_active_list) {
        const active_res = active_url.match(allow_active); // 判定是否允许访问
        if (active_res) {
          flag = true;
          break;
        }
      }
    }
    flag ? await next() : ctx.redirect(options.jump_login_url[module_name]);

  };
};

